<?php

namespace app\api\controller;

use app\api\service\LoginService;
use app\api\service\OrderService;
use app\api\service\SampleOrderService;
use app\BaseController;
use app\Response;
use think\facade\Request;

class Order extends BaseController
{
    /** 订单列表
    */
    public function orderList(){
        if (Request::isPost()) {
            $user_id = LoginService::getUserIdFromToken();
            if (!is_numeric($user_id)) {
                return $user_id;
            }
            $page = Request::post("page",1);
            $num = Request::post("num",10);
            $state = Request::post("state","");
            $type = Request::post("type","");
            $info = OrderService::orderListService($user_id,$page,$num,$state,$type);
            return $info;
        }
    }

    public function robOrder(){
        if (Request::isPost()) {
            $user_id = LoginService::getUserIdFromToken();
            if (!is_numeric($user_id)) {
                return $user_id;
            }
            $data = $this->request->post();
            try{
                $this->validate($data, 'RobOrder');
            }catch (\Exception $e){
                return Response::wrapError(0,$e->getMessage());
            }
            $a_id = Request::param("a_id");//快递时地址ID 自提时驿站ID
            $address = Request::param("address");
            $res = OrderService::robOrderService($user_id,$data,$a_id,$address);
            return $res;
        }
    }


    public function orderInfo(){
        if (Request::isPost()) {
            $user_id = LoginService::getUserIdFromToken();
            if (!is_numeric($user_id)) {
                return $user_id;
            }
            $id = Request::post("id");
            if(empty($id)){
                return Response::wrapError(0,"参数异常!");
            }
            $res = OrderService::orderInfoService($id,$user_id);
            return $res;
        }
    }


    /** 购物车下单确认页
    */
    public function cartRobOrderCheck(){
        if (Request::isPost()) {
            $user_id = LoginService::getUserIdFromToken();
            if (!is_numeric($user_id)) {
                return $user_id;
            }
            $cartInfo = Request::post("cart_info");
            $shi_type = Request::post("shi_type");
            $res = OrderService::cartRobOrderCheck($user_id,$cartInfo,$shi_type);
            return $res;
        }
    }

    /** 购物车提交订单换取支付信息
     */
    public function cartOrderWxPay(){
        $user_id = LoginService::getUserIdFromToken();
        if (!is_numeric($user_id)) {
            return $user_id;
        }
        $cartInfo = Request::post("cart_info");
        $shi_type = Request::post("type");
        $a_id = Request::post("a_id");
        $address = Request::post("address");
        $res = OrderService::cartOrderWxPayService($user_id,$cartInfo,$shi_type,$address,$a_id);
        return $res;
    }

    /** 单商品订单支付调起
     */
    public function orderWxPay(){
        if (Request::isPost()) {
            $user_id = LoginService::getUserIdFromToken();
            if (!is_numeric($user_id)) {
                return $user_id;
            }
            $id = Request::post("id");
            if(empty($id)){
                return Response::wrapError(0,"参数异常!");
            }
            $info = OrderService::orderWxPayService($id,$user_id);
            return $info;
        }
    }

    /*** 订单评价
    */
    public function evaluation(){
        if (Request::isPost()) {
            $user_id = LoginService::getUserIdFromToken();
            if (!is_numeric($user_id)) {
                return $user_id;
            }
            $id = Request::post("id","");
            $img_json = Request::post("img_json","");
            $content = Request::post("content","");
            $res = OrderService::evaluationService($user_id,$id,$img_json,$content);
            return $res;
        }
    }

    public function orderBalancePay(){
        if (Request::isPost()) {
            $user_id = LoginService::getUserIdFromToken();
            if (!is_numeric($user_id)) {
                return $user_id;
            }
            $id = Request::post("id");
            $pay_pw = Request::post("pay_pw");
            if(empty($id)){
                return Response::wrapError(0,"参数异常!");
            }
            if(empty($pay_pw)){
                return Response::wrapError(0,"请输入支付密码!");
            }
            $info = SampleOrderService::sampleOrderBalancePay($id,$user_id,$pay_pw);
            return $info;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
        }
    }

    /*** 订单确认收货
    */
    public function orderReceive(){
        if (Request::isPost()) {
            $user_id = LoginService::getUserIdFromToken();
            if (!is_numeric($user_id)) {
                return $user_id;
            }
            $id = Request::post("id");
            if(empty($id)){
                return Response::wrapError("订单信息获取异常!");
            }
            $res = OrderService::orderReceiveService($user_id,$id);
            return $res;
        }
    }

    public function orderRefund(){
        if (Request::isPost()) {
            $user_id = LoginService::getUserIdFromToken();
            if (!is_numeric($user_id)) {
                return $user_id;
            }
            $id = Request::post("id");
            if(empty($id)){
                return Response::wrapError("订单信息获取异常!");
            }
            $res = OrderService::orderReceiveService($user_id,$id);
            return $res;
        }
    }

    //售后列表
    public function afterSalesList(){
        if (Request::isPost()) {
            $user_id = LoginService::getUserIdFromToken();
            if (!is_numeric($user_id)) {
                return $user_id;
            }
            $page = Request::post("page",1);
            $num = Request::post("num",10);
            $state = Request::post("state","");
            $res = OrderService::afterSalesListService($user_id,$page,$num,$state);
            return $res;
        }
    }

    //售后详情
    public function afterSales(){
        if (Request::isPost()) {
            $user_id = LoginService::getUserIdFromToken();
            if (!is_numeric($user_id)) {
                return $user_id;
            }
            $id = Request::post("id");
            if(empty($id)){
                return Response::wrapError(0,"订单信息获取异常!");
            }
            $res = OrderService::orderAfterSaleService($user_id,$id);
            return $res;
        }
    }


}